<template>
  <div>
    <el-dialog title="活动背景音乐设置" :visible.sync="showEditPageDialog" center width="770px">
      <input style="margin-left:280px;margin-bottom:20px;" placeholder="请输入命名" v-model="formData.name">
      <div :v-model="formData">
        <el-upload action="" style="width:150px;margin-left:300px;" :limit="1" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeMusicUpload"  :http-request="music_upload">
        <el-button type="primary">点击上传</el-button>
      </el-upload>
      </div>
      <el-button type="danger" style="margin-left:220px;" @click="cancel()">取消</el-button>
      <el-button type="primary" style="margin-left:100px;" @click="confirm">完成</el-button>
    </el-dialog>
  </div>
</template>
<script>
import {edit,add,getList} from '@/api/thk/cms/music'
import { client } from "@/utils/alioss";
export default {
  data(){
    return{
       showEditPageDialog: false,
       fileList:[],
       formData:{
         id:1,
         name:'',
         music: null,
       }
    }
  },
  inject: ['reload'],
  methods:{
    confirm(){
      edit(this.$store.getters.token, this.formData).then((res) => {
        console.log(this.formData,'this.formData');
        console.log(res,'res');
        if (res.data.status) {
          this.$message.success("修改成功");
          this.$emit("getList");
          this.showEditPageDialog = false;
        } else {
          this.$message.error("修改失败！原因：" + res.data.msg);
          this.$emit("getList");
        }
      })
      this.reload()
    },
    cancel(){
      this.showEditPageDialog = false
    },
    showEditDialog(row){
      console.log(row,'showEditDialog')
      this.showEditPageDialog = !this.showEditPageDialog
      this.formData = row
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    beforeMusicUpload(file) {
      const isMp3 = file.type === "audio/mpeg" || "audio/mp3";
      console.log(file, "audio/mpeg");

      if (!isMp3) {
        this.$message.error("上传音频只能是mp3格式");
      }
      return isMp3;
    },
    music_upload(file) {
      const that = this;
      var fileName = "monster_music" + file.file.uid;
      console.log(fileName,'fileName');
      client()
        .put(fileName, file.file)
        .then((result) => {
          console.log(result, "result");
          this.formData.music = result.url;
          this.$message.success("音频上传成功！");
        })
        .catch((err) => {
          this.$message.error("音频上传失败，原因" + err);
        });
      console.log(this.formData,'this.formData');
    }
  }
}
</script>
<style>
.viptime{
  font-size: 20px;
  margin-bottom: 30px;
}
.vipprice{
  font-size: 20px;
  margin-bottom: 30px;
}
.vipcheap{
  font-size: 20px;
  margin-bottom: 30px;
}
</style>